<?php
//namespace lib;

class Template
{
    public function loaddir()
    {
        global $conf;
        $template = !empty($conf['template_web']) ? $conf['template_web'] : 'default';
        if (checkmobile() && !empty($conf['template_phone'])) {
            $template = $conf['template_phone'];
        }
        $template = is_dir(TEMPLATE_ROOT . $template . '/') ? $template : 'default';
        return $template;
    }

    public function getList()
    {
        $dir = TEMPLATE_ROOT;
        $dirArray[] = null;
        if (false != ($handle = opendir($dir))) {
            $i = 0;
            while (false !== ($file = readdir($handle))) {
                if ($file != "." && $file != ".." && !strpos($file, ".")) {
                    $dirArray[$i] = $file;
                    $i++;
                }
            }
            closedir($handle);
        }
        return $dirArray;
    }

    public function load($name = 'index')
    {
        $template = $this->loaddir();
        if (!preg_match('/^[a-zA-Z0-9]+$/', $name)) {
            exit('error');
        }

        $filename = TEMPLATE_ROOT . $template . '/' . $name . '.php';
        $filename_default = TEMPLATE_ROOT . 'default/' . $name . '.php';
        if (file_exists($filename)) {
            return $filename;
        } elseif (file_exists($filename_default)) {
            return $filename_default;
        } else {
            exit('Template file not found');
        }
    }

    public function loadConfig()
    {
        $template = $this->loaddir();
        $filename = TEMPLATE_ROOT . $template . '/config.php';
        if (file_exists($filename)) {
            include $filename;
            return $template_info;
        } else {
            return false;
        }
    }

    public function loadRoute()
    {
        $template = $this->loaddir();
        $filename = TEMPLATE_ROOT . $template . '/config.php';
        if (file_exists($filename)) {
            include $filename;
            $var = checkmobile() && isset($template_route_p) ? $template_route_p : $template_route;
            return $var;
        } else {
            return false;
        }
    }

    public function exists($template)
    {
        $filename = TEMPLATE_ROOT . $template . '/index.php';
        if (file_exists($filename)) {
            return true;
        } else {
            return false;
        }
    }

    public function getBackground()
    {
        global $conf, $CACHE;
        if (date("Ymd") == $conf['ui_bing_date']) {
            $background_image = $conf['ui_backgroundurl'];
            if (checkmobile() == true) {
                $background_image = str_replace('1920x1080', '768x1366', $background_image);
            }
        } else {
            $url = 'https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN';
            $bing_data = get_curl($url);
            $bing_arr = json_decode($bing_data, true);
            if (!empty($bing_arr['images'][0]['url'])) {
                $background_image = '//cn.bing.com' . $bing_arr['images'][0]['url'];
                saveSetting('ui_backgroundurl', $background_image);
                saveSetting('ui_bing_date', date("Ymd"));
                $CACHE->clear();
                if (checkmobile() == true) {
                    $background_image = str_replace('1920x1080', '768x1366', $background_image);
                }
            }
        }
        return $background_image;
    }
}
